# -*- coding: latin-1 -*-
from gestion.fondos.models import *
from gestion.fondos.views import *
from django.contrib import admin
from itertools import *
from tjango.modules.models import system

system.reg_app("fondos","Fondos")

					
class ItemCheque(models.Model):
	banco = AutocompleteField(Banco, ['nombre'])
	numero = models.CharField(max_length=20)
	cheque = ForeignKey(Cheque, related_name="entregas")
	importe = CalculatedField(source = ['cheque','banco'],read_only=False,server = "Cheque.objects.get(numero=cheque,banco=banco).importe")
	vencimiento = CalculatedField(source = ['cheque'],read_only=False,server = "Cheque.objects.get(numero=cheque).get_fecha()")
	a_pagar = models.ForeignKey(FormaDePago, related_name="cheques")
	def save(self):
		try:
			self.cheque = Cheque.objects.get(numero=self.numero)
		except:
			self.cheque = Cheque(banco = self.banco,numero = self.numero,vencimiento=self.vencimiento, importe = self.importe)
			
			self.cheque.save()
		self.cheque_id = self.cheque.id
		super(ItemCheque, self).save()


	
	


#ItemCheque.objects.filter(a_pagar__pago__compra__id__isnull=False)
#Cheque.objects.filter(entregas__a_pagar__pago__compra__factura__isnull=False)        
class ItemCheque_Inline(admin.TabularInline): 
	name = "ItemCheque_Inline"
	model = ItemCheque 
	fields= ('banco', 'numero', 'importe', 'vencimiento')
	extra = 10
	min_num_in_admin = 10
#	fk_name = "tes"



system.apps["fondos"].reg_item(Banco,"Bancos")
system.apps["fondos"].reg_item(Cheques_activos,"Cheques activos",query=True)		


